#!/usr/bin/env rc

if(! ls /livecd >/dev/null >[2=1]) {
	echo Needed directories not found!
	exit 1
}
PATHBARE=/livecd/reps
PATHPRGS=/livecd/prgs
PATHDOCS=/livecd/docs
PATHSSH=/livecd/ssh

## copy template
cp -r /usr/share/parabolaiso/configs/profile archlive

## configure packages

# add packages I want
cat >>archlive/packages.x86_64 <<EOF
read-edid
xvidtune
rsync
sshfs
foremost
extundelete
testdisk
ncmpc
ttf-droid
freetype2
pkg-config
gcc
make
gnupg
git
networkmanager
python2-dbus
wireshark-cli
surf
acpi
acpid
slock
tmux
flashrom
vim
redshift
xorg-server
xorg-xinit
xorg-xprop
xorg-xrandr
xorg-xfontsel
xorg-xrdb
xorg-xset
xorg-xsetroot
xorg-setxkbmap
xf86-input-evdev
xf86-video-intel
EOF

# remove packages I do not need:
sed '/refind-efi/d' < archlive/packages.x86_64 > archlive/packages.x86_64.tmp
mv archlive/packages.x86_64.tmp archlive/packages.x86_64

sed '/zsh/d' < archlive/packages.both > archlive/packages.both.tmp
sed '/sudo/d' < archlive/packages.both.tmp > archlive/packages.both
sed '/irssi/d' < archlive/packages.both > archlive/packages.both.tmp
sed '/lynx/d' < archlive/packages.both.tmp > archlive/packages.both
sed '/mc/d' < archlive/packages.both > archlive/packages.both.tmp
sed '/grub/d' < archlive/packages.both.tmp > archlive/packages.both
rm archlive/packages.both.tmp

## add etc files

# skel (home directory content for all users)
cp -r /etc/skel archlive/root-image/etc

cat >archlive/root-image/etc/skel/installPlan9.sh <<EOF
#!/bin/sh
git clone /reps/plan9 /usr/local/plan9
cd /usr/local/plan9
./INSTALL
EOF

# pseudo anonymous git config
cat >archlive/root-image/etc/skel/.gitconfig <<EOF
[user]
	email = livecd@do-not-mail-me.com
	name = LiveCD Author
[core]
	excludesfile = ~/.gitignore_global
[diff]
	tool = vimdiff
[push]
	default = simple
[alias]
	cached = diff --cached
	save = stash save -u
	restore = stash pop
	hist = log --graph
# use a shell function to achieve alias with parameter
	sh = "!f() { git show stash@{$$1}; }; f"
EOF

# live cd ssh key
mkdir archlive/root-image/etc/skel/.ssh
cp $PATHSSH/* archlive/root-image/etc/skel/.ssh

# TODO /etc/skel/bin additions (surf control scripts, ...)

# copy some handy documentation for live environment
cp -r $PATHDOCS/* archlive/root-image/etc/skel

# Xorg configuration
mkdir archlive/root-image/etc/X11

cat >archlive/root-image/etc/X11/Xwrapper.config <<EOF
needs_root_rights=yes
EOF

mkdir archlive/root-image/etc/X11/xorg.conf.d
cat >archlive/root-image/etc/X11/xorg.conf.d/10-keyboard.conf <<EOF
Section "InputClass"
	Identifier "keyboard"
	Driver "evdev"
	MatchIsKeyboard "on"
	Option "XkbLayout" "gb"
EndSection
EOF

cat >archlive/root-image/etc/X11/xorg.conf.d/11-mouse.conf <<EOF
Section "InputClass"
	Identifier "mouse"
	Driver "evdev"
EndSection
EOF

# may be a nice idea to make live environment aware of home network
cp /etc/hosts archlive/root-image/etc/hosts

# keymap and font for terminal
cat >archlive/root-image/etc/vconsole.conf <<EOF
KEYMAP=us
FONT=Lat2-Terminus16
EOF

# locale
cat >archlive/root-image/etc/locale.conf <<EOF
LANG="en_US.UTF-8"
LC_COLLATE="C"
LC_TIME="en_US.UTF-8"
EOF

# copy tmux config
cp /etc/tmux.conf  archlive/root-image/etc/tmux.conf

# replaced lynx with elinks thus change message of the day
sed 's/lynx/elinks/g' < archlive/root-image/etc/motd > archlive/root-image/etc/motd.tmp
mv archlive/root-image/etc/motd.tmp archlive/root-image/etc/motd

# copy bare repositories
cp -r $PATHBARE archlive/root-image/reps
# copy suckless programs (dwm,st,dmenu)
cp -r $PATHPRGS/* archlive/root-image/root

# run on startup
sed '/localtime/s_UTC_Europe/Berlin_g' < archlive/root-image/root/customize_root_image.sh > archlive/root-image/root/customize_root_image.sh.tmp
sed 's_/usr/bin/zsh_/bin/bash_g' < archlive/root-image/root/customize_root_image.sh.tmp > archlive/root-image/root/customize_root_image.sh
sed '/sudo/d' < archlive/root-image/root/customize_root_image.sh > archlive/root-image/root/customize_root_image.sh.tmp
mv archlive/root-image/root/customize_root_image.sh.tmp archlive/root-image/root/customize_root_image.sh
chmod +x archlive/root-image/root/customize_root_image.sh

cat >>archlive/root-image/root/customize_root_image.sh <<EOF

mkdir /usr/local/plan9
chown parabola /usr/local/plan9

cd /root/dwm && make && make install && make clean
cd /root/dmenu && make && make install && make clean
cd /root/st && make && make install && make clean
EOF

## edit build.sh
sed 's#initcpio_dir=.*#initcpio_dir=/usr/lib/initcpio#' < archlive/build.sh > archlive/build.sh.tmp
sed 's/for arch in i686 x86_64/for arch in x86_64/' < archlive/build.sh.tmp > archlive/build.sh
rm archlive/build.sh.tmp
